ReactJS Ultra For Beginners
🔷 TypeScript in React
What is TypeScript in React?
- Adds static type checking to React applications
- Catches errors at compile time instead of runtime
- Provides better IntelliSense and developer experience
- Makes code more maintainable and self-documenting
📝 Basic Setup
// Component with TypeScript interface UserProps { name: string; age: number; email?: string; // Optional prop } function User({ name, age, email }: UserProps) { return ( <div> <h2>{name}</h2> <p>Age: {age}</p> {email && <p>Email: {email}</p>} </div> ); } // Usage <User name="John" age={25} email="john@example.com" />;
🔑 Core Concepts
1. Props Interface:
interface ButtonProps { text: string; onClick: () => void; variant?: "primary" | "secondary" | "danger"; disabled?: boolean; size?: "small" | "medium" | "large"; } function Button({ text, onClick, variant = "primary", disabled = false, size = "medium", }: ButtonProps) { return ( <button onClick={onClick} disabled={disabled} className={`btn btn-${variant} btn-${size}`} > {text} </button> ); }
2. State with TypeScript:
interface User { id: number; name: string; email: string; } function UserProfile() { const [user, setUser] = (useState < User) | (null > null); const [loading, setLoading] = useState < boolean > false; const [error, setError] = (useState < string) | (null > null); const fetchUser = async (id: number): Promise<void> => { setLoading(true); try { const response = await fetch(`/api/users/${id}`); const userData: User = await response.json(); setUser(userData); } catch (err) { setError(err instanceof Error ? err.message : "An error occurred"); } finally { setLoading(false); } }; return ( <div> {loading && <p>Loading...</p>} {error && <p>Error: {error}</p>} {user && ( <div> <h1>{user.name}</h1> <p>{user.email}</p> </div> )} </div> ); }
3. Event Handlers:
interface FormData { name: string; email: string; age: number; } function UserForm() { const [formData, setFormData] = useState < FormData > { name: "", email: "", age: 0, }; const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>): void => { const { name, value } = e.target; setFormData((prev) => ({ ...prev, [name]: name === "age" ? parseInt(value) || 0 : value, })); }; const handleSubmit = (e: React.FormEvent<HTMLFormElement>): void => { e.preventDefault(); console.log("Form submitted:", formData); }; return ( <form onSubmit={handleSubmit}> <input type="text" name="name" value={formData.name} onChange={handleInputChange} placeholder="Name" /> <input type="email" name="email" value={formData.email} onChange={handleInputChange} placeholder="Email" /> <input type="number" name="age" value={formData.age} onChange={handleInputChange} placeholder="Age" /> <button type="submit">Submit</button> </form> ); }
🎯 Common Patterns
Generic Components:
interface ListProps<T> { items: T[]; renderItem: (item: T, index: number) => React.ReactNode; keyExtractor: (item: T) => string | number; } function List<T>({ items, renderItem, keyExtractor }: ListProps<T>) { return ( <ul> {items.map((item, index) => ( <li key={keyExtractor(item)}>{renderItem(item, index)}</li> ))} </ul> ); } // Usage interface User { id: number; name: string; } function UserList() { const users: User[] = [ { id: 1, name: "John" }, { id: 2, name: "Jane" }, ]; return ( <List items={users} renderItem={(user) => <span>{user.name}</span>} keyExtractor={(user) => user.id} /> ); }
Custom Hooks with TypeScript:
interface UseCounterReturn { count: number; increment: () => void; decrement: () => void; reset: () => void; } function useCounter(initialValue: number = 0): UseCounterReturn { const [count, setCount] = useState < number > initialValue; const increment = (): void => setCount((prev) => prev + 1); const decrement = (): void => setCount((prev) => prev - 1); const reset = (): void => setCount(initialValue); return { count, increment, decrement, reset }; } // Usage function Counter() { const { count, increment, decrement, reset } = useCounter(10); return ( <div> <p>Count: {count}</p> <button onClick={increment}>+</button> <button onClick={decrement}>-</button> <button onClick={reset}>Reset</button> </div> ); }
Context with TypeScript:
interface ThemeContextType { theme: "light" | "dark"; toggleTheme: () => void; } const ThemeContext = (createContext < ThemeContextType) | (undefined > undefined); function ThemeProvider({ children }: { children: React.ReactNode }) { const [theme, setTheme] = (useState < "light") | ("dark" > "light"); const toggleTheme = (): void => { setTheme((prev) => (prev === "light" ? "dark" : "light")); }; return ( <ThemeContext.Provider value={{ theme, toggleTheme }}> {children} </ThemeContext.Provider> ); } function useTheme(): ThemeContextType { const context = useContext(ThemeContext); if (context === undefined) { throw new Error("useTheme must be used within a ThemeProvider"); } return context; }
🔧 Advanced Types
Union Types:
type Status = "idle" | "loading" | "success" | "error"; interface ApiState<T> { data: T | null; status: Status; error: string | null; } function useApi<T>(url: string): ApiState<T> { const [state, setState] = useState < ApiState < T >> { data: null, status: "idle", error: null, }; // ... implementation return state; }
Utility Types:
interface User { id: number; name: string; email: string; age: number; } // Make all properties optional type PartialUser = Partial<User>; // Make specific properties required type CreateUser = Pick<User, "name" | "email"> & Partial<Pick<User, "age">>; // Omit specific properties type UserWithoutId = Omit<User, "id">; // Function type type EventHandler<T = HTMLElement> = (event: React.MouseEvent<T>) => void;
Conditional Types:
type ButtonProps<T extends 'button' | 'a'> = { as?: T; children: React.ReactNode; } & (T extends 'button' ? React.ButtonHTMLAttributes<HTMLButtonElement> : React.AnchorHTMLAttributes<HTMLAnchorElement> ); function Button<T extends 'button' | 'a' = 'button'>({ as, children, ...props }: ButtonProps<T>) { const Component = as || 'button'; return <Component {...props}>{children}</Component>; }
⚠️ Best Practices
- Use interfaces for object shapes - more extensible than types
- Use type for unions and primitives - more appropriate
- Always type your props - even for simple components
- Use generic constraints - limit what types can be passed
- Handle undefined/null properly - use optional chaining and nullish coalescing
- Use strict mode - enable strict TypeScript configuration
- Avoid
anytype - useunknownor proper types instead
🔧 Configuration
tsconfig.json:
{ "compilerOptions": { "target": "ES2020", "lib": ["DOM", "DOM.Iterable", "ES6"], "allowJs": true, "skipLibCheck": true, "esModuleInterop": true, "allowSyntheticDefaultImports": true, "strict": true, "forceConsistentCasingInFileNames": true, "noFallthroughCasesInSwitch": true, "module": "ESNext", "moduleResolution": "node", "resolveJsonModule": true, "isolatedModules": true, "noEmit": true, "jsx": "react-jsx" }, "include": ["src"] }
111 / 111 videos100% complete
Course Completed!
Great job! You've finished all videos.
Welcome to the Course
Intro to The Course
3:33How to Learn This Course
3:02What is React and Why You Should Learn It?
4:14Getting Started with React
Installing Nodejs
3:03Setting up a React Development Environment
4:30VSCode Extensions
3:17JavaScript Refresher - All JS You Need To Know Before React
Functions
5:39Ternary and Logical Operators
7:21Objects and Arrays
7:11Map and Filter Functions
7:15Array Function Key Prop
3:13String Templating
4:27Basic React Concepts
React Project Folder Structure
6:55An Intro To JSX
10:33What is the App Component?
2:46How to use Components?
10:23Lists in React
9:45CSS in React
9:10Component Project - Build A Simple Website
First React Project
3:34Making A Navbar
7:11Displaying Images
7:11Making the About Us Section
2:35Creating A Footer
3:38Intro To States In React
What is the useState Hook?
10:38What is the useState Hook? Part 2
4:42What is the useState Hook? Part 3
14:50Intro React Project - Task Tracker App
Beginner Project - Task Tracker App
35:03React Component Lifecycle
The Virtual Dom
1:50React Component Lifecycle
1:42The useEffect Hook
15:40The useEffect Hook Part 2
8:41Routing in React
React Router
14:05React Router Part 2
14:28Advanced Forms In React
Forms in React - React Hook Form
25:36State Management
Intro To State Management - Global States
18:37Beginner Project - Build A Blog App
Intermediate Project - Build A Blog App
40:40Intermediate Project - Build A Blog App Part 2
26:57Other Useful React Hooks
Rules of Hooks
4:05Learn the useReducer Hook
9:54Learn the useRef Hook
12:03Making Your Own Custom Hook
12:10Advanced State Management - Zustand
Why Zustand?
5:22Intro to Zustand
11:08Zustand Subscriptions
9:01Advanced Data Handling With React Query
Why React Query?
6:18The useQuery Hook
16:38The useMutation Hook
8:45Intermediate Project - Build A FullStack Game Manager
What are we building?
2:54Project Demo
2:26Project Structure
5:28Setup REST API
12:43Set Up React Project
6:02Setup the Project Routes
9:12Fetch List of Games
8:13Display List of Games
1:44Create Game Page
20:03Invalidating Queries
5:34Update Game Page
6:05Fetch Game By Id
3:47Reseting the Form
7:21Update Game Endpoint
4:28Update Game Mutation
2:49Invalidating More Queries
2:58Creating the Delete Game Endpoint
1:58Adding the Delete Button
5:28Creating the Theme Manager
11:59Creating the 404 Page Not Found
2:19Thinking In React - Best Practices
Rules of React
4:01Keeping Components Focused
5:08What Are Class Components?
4:19Transition to TypeScript in React
Why Typescript?
3:16Basic Typescript Types
6:23Functions in Typescript
2:41Custom Typescript Types
4:34Unions
3:33Interfaces
5:31Intersections
1:54Enums
2:40Setting Up Typescript in React
4:05Props using Typescript
3:39States using Typescript
3:30Forms using Typescript
2:02Zustand using Typescript
2:16